<?php

if (defined('NO_DIRECT') && $session['user']['role'] == '2') {
	if (isset($_POST['sponsor_title'])) {
		$sponsor_id = intval($_POST['sponsor_id']);
		$sponsor_title = $_POST['sponsor_title'];
		$sponsor_title = preg_replace('/\s+/',' ',$sponsor_title);
		$sponsor_title = trim($sponsor_title);
		$sponsor_title = amankan($sponsor_title);
		$sponsor_link = $_POST['sponsor_link'];
		$sponsor_title = preg_replace('/\s+/',' ',$sponsor_title);
		$sponsor_title = trim($sponsor_title);
		$sponsor_title = amankan($sponsor_title);
		$sponsor_place = intval($_POST['sponsor_place']);
		$set = '';
		$sponsor = db_result('SELECT * FROM sponsors WHERE sponsor_id = '.$sponsor_id);
		$is_legal = true;
		$file_path = ROOT_PATH.'sponsor'.DIRECTORY_SEPARATOR;
		$error = '';
		if ($sponsor === false) {
			// it means new sponsor
			if (strlen($sponsor_title) == 0) {
				$is_legal = false;
				$error .= '-Judul sponsor tidak boleh kosong';
			}
			if (strlen($sponsor_link) == 0) {
				$is_legal = false;
				$error .= '-Link sponsor tidak boleh kosong';
			}
			if ($_FILES['sponsor_image']['error'] == UPLOAD_ERR_NO_FILE) {
				$is_legal = false;
				$error .= '-Gambar tidak boleh kosong';
			} elseif ($_FILES['sponsor_image']['error'] != UPLOAD_ERR_OK) {
				$is_legal = false;
				$error .= '-Unknown error type '.$_FILES['sponsor_image']['error'];
			} elseif ($_FILES['sponsor_image']['type'] != 'image/jpeg') {
				$is_legal = false;
				$error .= '-Gambar hanya boleh jpg/jpeg';
			}
			if ($is_legal) {
				db_query('UPDATE sponsors SET sponsor_order = 0 WHERE sponsor_order = '.$sponsor_place);
				$sponsor_id = db_insert('INSERT INTO sponsors (sponsor_title,sponsor_link,sponsor_order) VALUES ("'.$sponsor_title.'","'.$sponsor_link.'",'.$sponsor_place.')');
			}
		} else {
			// it means update sponsor
			$set = ' SET sponsor_place = '.$sponsor_place.' ';
			if (strlen($sponsor_title) > 0 || strlen($sponsor_link) > 0) {
				$set .= ' , ';
				if (strlen($sponsor_title) > 0) {
					$set .= 'sponsor_title = '.$sponsor_title.' ';
					if (strlen($sponsor_link) > 0) {
						$set .= ' , ';
					}
				}
				if (strlen($sponsor_link) > 0) {
					$set .= 'sponsor_link = '.$sponsor_link.' ';
				}
			}
			if ($_FILES['sponsor_image']['error'] != UPLOAD_ERR_OK && $_FILES['sponsor_image']['error'] != UPLOAD_ERR_NO_FILE) {
				$is_legal = false;
				$error .= '-Unknown error type '.$_FILES['sponsor_image']['error'];
			} elseif ($_FILES['sponsor_image']['type'] != 'image/jpeg') {
				$is_legal = false;
				$error .= '-Gambar hanya boleh jpg/jpeg';
			}
			if ($is_legal) {
				db_query('UPDATE sponsors SET sponsor_order = 0 WHERE sponsor_order = '.$sponsor_place);
				db_query('UPDATE sponsors '.$set.' WHERE sponsor_id = '.$sponsor_id);
			}
		}
		// next imaging
		if ($is_legal) {
			if ($_FILES['sponsor_image']['error'] != UPLOAD_ERR_NO_FILE) {
				move_uploaded_file($_FILES['file']['tmp_name'],$file_path.$sponsor_id.'.jpg'));
			}
		}
	} elseif (isset($_POST['sponsor_delete'])) {
		$sponsor_delete = intval($_POST['sponsor_delete']);
		db_query('DELETE FROM sponsors WHERE sponsor_id = '.$sponsor_delete);
		// next delete image
		$file_path = $file_path.$sponsor_delete.'.jpg';
		if (file_exists($file_path)) {
			unlink($file_path);
		}
	}
}